<template>
  <a-modal destroyOnClose :visible="visible" :width="920" :maskClosable="false" @cancel="cancelModal(false)" class="PositionRecResume" :footer="null">
    <div v-if="spin">
      <a-spin />
    </div>
    <template v-else>
      <template slot="title">
        <!-- <i class="iconfont icon-fanhui" style="cursor:pointer"/> -->
        <a-icon type="arrow-left" v-if="((searchBtn > 0 && switchConfig) || (searchBtn > 1 && !switchConfig)) && (!defaultBtn || searchBtn > defaultBtn)" @click="back"/>
        {{$t(title)}}
      </template>
      <resume-cnki :positionInfo="positionInfo" :basicData="basicData" v-if="cunzai" :loading="loading" :switchConfig="switchConfig" ref="resumeCnki" v-show="searchBtn < 2" :searchBtn="searchBtn === 0" @searchResume="searchResume" @upFile="upFile" @loading="(flag) => loading1 = flag" :messages="messages" :chekedLoading="chekedLoading"/>
      <position-rec-desc v-if="searchBtn > 1" :nodesc="true">
        <template slot="content">
          <resume-update :isPending="isPending" :positionInfo="positionInfo" ref="resumeUpdate"  v-show="searchBtn === 2" :formData="formData" :enterpriseId="enterpriseId" :positionId="positionId" :fileData="fileData"/>
          <position-rec-reason :positionInfo="positionInfo" ref="reason" :searchBtn="searchBtn" :resumeData="resumeData"  v-show="searchBtn === 3" :enterpriseId="enterpriseId" :positionId="positionId"/>
        </template>
      </position-rec-desc>
      <div class="ant-modal-footer" v-if="searchBtn">
        <a-button key="back" @click="cancelModal(false)">
          {{$t('common.cancel')}}
        </a-button>
        <a-button key="submit" type="primary" :loading="loading || loading1" @click="cancelModal(true)">
          {{getBtnName}}
        </a-button>
      </div>
    </template>
  </a-modal>
</template>

<script>
import { mapState } from 'vuex'
import { recommend, resumeCheck, resumeCheckConfig } from '@/api/resume'
import ResumeCnki from '@/components/resume/ResumeCnki'
import PositionRecDesc from '@/components/position/PositionRecDesc'
import ResumeUpdate from '@/components/resume/ResumeUpdate'
import PositionRecReason from '@/components/position/PositionRecReason'
export default {
  name: 'PositionRecResume',
  // props: ['visible', 'positionId', 'enterpriseId', 'sourcePid', 'positionInfo', 'searchType', 'defaultBtn'],
  props: {
    visible: {
      type: Boolean,
      require: false
    },
    isPending: {
      type: Boolean,
      require: false
    },
    positionId: { // 原因、查重、解析文件接口 职位id
      type: String,
      require: true
    },
    enterpriseId: { // 查重配置、原因、查重、解析 企业id
      type: String,
      require: true
    },
    sourcePid: { // 只查重
      type: String,
      require: false
    },
    positionInfo: { // 判定isRPO
      type: Object,
      require: false,
      default: () => {}
    },
    searchType: { // 企业职位和个人职位
      type: String,
      require: false
    },
    defaultBtn: { // 进入后默认指定流程节点，指定后不不可跳转至之前节点
      type: Number,
      require: false
    }
  },
  components: { ResumeCnki, PositionRecReason, ResumeUpdate, PositionRecDesc },
  data () {
    return {
      resumeData: {},
      loading1: false,
      title: 'candidateApplication.recommend_cadidate',
      chekedLoading: false,
      messages: '',
      loading: false,
      spin: true,
      // current: 0,
      searchBtn: 3,
      // filePath: '/usr/local/MicroServices/HRS-jobboardCandidates-beta/uploads/clients/tmpResume/null/C++开发工程师1_小明_1574668823545.docx',
      fileData: null,
      formData: null,
      switchConfig: false,
      cunzai: true,
      resumeId: '',
      basicData: {}
    }
  },
  computed: {
    ...mapState({
      userInfo: state => state.user.userInfo
    }),
    getBtnName () {
      return (this.positionInfo.isRPO === 'Y' && !this.switchConfig && this.searchBtn < 2) || this.searchBtn === 3 ? this.$t('common.submit') : this.$t('common.next_step')
    }
  },
  watch: {
    visible: {
      handler (newValue, oldValue) {
        if (newValue) {
          this.spin = true
          this.formData = null
          this.chekedLoading = false
          this.searchBtn = this.defaultBtn || 0
          this.resumeId = ''
          this.filePath = ''
          this.messages = ''
          this.fileData = null
          if (!this.defaultBtn) {
            // this.checkConfig()
          } else {
            this.formData = this.positionInfo.resume
            this.fileData = {
              updateResumeIds: this.positionInfo.updateResumeIds
            }
            this.spin = false
          }
        }
      },
      immediate: true
    }
  },
  created () {
  },
  methods: {
    back () {
      if (this.searchBtn === 1) {
        this.cunzai = false
        this.loading = false
        this.$nextTick(() => {
          this.cunzai = true
        })
      }
      --this.searchBtn
    },
    async checkConfig () {
      let res = await resumeCheckConfig({ enterpriseId: this.enterpriseId, isRPO: this.positionInfo.isRPO || 'N' })
      if (res.code === 200) {
        this.switchConfig = res.data.switch
        if (!res.data.switch) {
          this.searchBtn = 1
        }
      }
      this.spin = false
    },
    upFile (fileData) {
      // this.filePath = filePath
      this.fileData = fileData
    },
    // async candidateData (data) {
    //   let params = {
    //     resume: data,
    //     resumeId: this.resumeId
    //   }
    //   let res = await saveResume(params)
    //   if (res.code === 200) {
    //     this.resumeId = res.resumeId
    //     this.searchBtn = ++this.searchBtn // 跳转到推荐原因
    //   } else {
    //     this.$message.error(res.message)
    //   }
    // },
    async recReason (data) {
      this.loading = true
      let params = {
        resumeId: this.resumeId,
        positionId: this.positionId,
        isRPO: this.positionInfo.isRPO || 'N',
        ...data
      }
      if (this.isPending) {
        params.analyzerRecordId = this.positionInfo.id
      }
      let res = await recommend(params)
      if (res.code === 200) {
        this.$message.success(res.message)
        this.$emit('close', { type: true })
      } else {
        this.$message.error(res.message)
      }
      this.loading = false
    },
    async resumeChecks (data) {
      let params = {
        positionId: this.positionId,
        enterpriseId: this.enterpriseId,
        sourcePid: this.sourcePid,
        isRPO: this.positionInfo.isRPO || 'N',
        ...data
      }
      let res = await resumeCheck(params)
      if (res && res.code === 200) {
        if (res.success) {
          // 当前页面保存  姓名 邮箱 电话
          this.basicData = data
          this.searchBtn = ++this.searchBtn // 跳转到推荐原因
        }
        this.messages = res.messages
      } else {
        res && this.$message.error(res.message)
      }
      this.chekedLoading = false
    },
    async sendFileList () {
      let data = {
        positionId: this.positionId,
        enterpriseId: this.enterpriseId,
        filePaths: this.fileData
      }
      let res = await this.$refs.resumeCnki.getFileListSave(data)
      if (res && res.code === 200) {
        // 弹出等待框
        this.$emit('close', { type: 'model', callbackId: res.data.callbackId, fileSize: res.data.fileSize })
        // this.$router.push(`/position/positionEditing?positionId=${this.positionInfo.id}&searchType=${this.searchType}&enterpriseId=${this.positionInfo.enterpriseId}&sourcePid=${this.positionInfo.sourcePid}`)
      }
    },
    async searchResume () {
      if (this.searchBtn === 0) {
        this.chekedLoading = true
        this.resumeId = ''
        this.fileData = null
        this.messages = ''
        let data = await this.$refs.resumeCnki.resumeChecks() // 获取编辑页面的整理好的数据
        if (data) {
          this.resumeChecks(data) // 上送数据
        } else {
          // this.$message.error('请填写必填字段！')
          this.chekedLoading = false
        }
      } else if (this.searchBtn === 1) { // 获取解析数据后跳转到简历信息编辑页
        if (!this.fileData) {
          this.$message.error(`${this.$t('candidateApplication.choose_file')}！`)
          return
        }
        this.loading = true
        if (this.positionInfo.isRPO === 'Y' && !this.switchConfig && (this.fileData.length > 1 || this.fileData[0].name.indexOf('.zip') !== -1)) {
          this.sendFileList()
        } else {
          if (this.positionInfo.isRPO === 'Y' && !this.switchConfig) {
            this.fileData = this.fileData[0]
          }
          let formData = await this.$refs.resumeCnki.getFileSave(this.fileData, this.basicData)
          if (formData && formData.code === 200) {
            this.formData = formData.data
            this.searchBtn = ++this.searchBtn
          } else if (!this.switchConfig) {
            this.searchBtn = ++this.searchBtn // 跳转到推荐原因
          }
        }
        this.loading = false
      } else if (this.searchBtn === 2) { // 提交简历信息编辑信息
        let data = await this.$refs.resumeUpdate.sendData() // 获取编辑页面的整理好的数据
        if (data && data.resumeId) {
          this.resumeData = data.values
          this.resumeId = data.resumeId
          this.searchBtn = ++this.searchBtn // 跳转到推荐原因
          // this.candidateData(data) // 上送数据
        }
        // else {
        //   this.$message.error('请填写必填字段！')
        // }
      } else if (this.searchBtn === 3) { // 推荐理由提交
        let data = await this.$refs.reason.sendData()
        if (data) {
          this.recReason(data) // 上送数据
        } else {
          this.$message.error(`${this.$t('common.confirm_require')}！`)
        }
      } else {
        this.searchBtn = ++this.searchBtn
      }
    },
    cancelModal (flag) {
      if (flag && this.searchBtn <= 3) {
        this.searchResume()
        return
      }
      this.$emit('close', {})
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '../../styles/var.styl'
</style>
<style lang="stylus">
.PositionRecResume
  :global(.ant-modal-content .ant-modal-body)
    padding 24px 0 0
  .ant-modal-footer
    padding 16px 24px
</style>
